package com.wenyu.wenyuyuedu.adapter.circleAdapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.homepage.InvitationDeatil_HeadViewGridView_Viewpager_Activity;
import com.wenyu.wenyuyuedu.base.ListBaseAdapter;
import com.wenyu.wenyuyuedu.bean.circle.CircleDetailBean;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.ListViewAndListView;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;
import com.wenyu.wenyuyuedu.utils.TimeStyle;
import com.wenyu.wenyuyuedu.view.CircleImageView;
import com.wenyu.wenyuyuedu.view.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

import me.chensir.expandabletextview.ExpandableTextView;

/**
 * Created by Administrator on 2017/3/16.
 * 发现--未加入书圈--的全部的帖子
 */

public class FindAllDetailAdapter extends ListBaseAdapter<CircleDetailBean.InfoBeanX.InfoBean> {
    private Context context;
    private List<CircleDetailBean.InfoBeanX.InfoBean.ForumBookmarkBean> forumBookmark;
    private Intent intent = new Intent();
    private List<String> images;
    private String circle_uid;

    public FindAllDetailAdapter(Context mContext) {
        super(mContext);
        this.context = mContext;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.mycircle_find_add_item, parent, false);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        CircleDetailBean.InfoBeanX.InfoBean infoBean = mDataList.get(position);
        MyViewHolder holderView = (MyViewHolder) holder;
        if (infoBean != null) {
            if (infoBean.getIcon_path() != null) {
                Glide.with(context)
                        .load(infoBean.getIcon_path())
                        .error(R.drawable.login_logo)
                        .into(holderView.circleImageView);
            }
            /*发布内容*/
            holderView.tv.setText(infoBean.getContent());
            /*昵称*/
            holderView.hoeme_all_nickname.setText(infoBean.getMember().getNickname());
            /*发布时间*/
            String create_time = infoBean.getCreate_time();
            String s = TimeStyle.publishTime(create_time);
            holderView.home_all_releasetime.setText(s);
            /*获取点赞人数*/
            forumBookmark = infoBean.getForumBookmark();
            int size = forumBookmark.size();
            if (size > 0) {
                holderView.invitation_show_zan.setVisibility(View.VISIBLE);
                String sZan = AlertLikeCount2(infoBean.getForumBookmark());
                holderView.ll_myclicle_detail_contentzan.setText(sZan);
                holderView.ll_myclicle_detail_contentzan.setVisibility(View.VISIBLE);
            } else {
                holderView.invitation_show_zan.setVisibility(View.GONE);
                holderView.ll_myclicle_detail_contentzan.setVisibility(View.GONE);
            }
            holderView.invitation_show_zan.setVisibility(size > 0 ? View.VISIBLE : View.INVISIBLE);
            /*5个赞以上显示神和打赏的图片*/
            String support_num = infoBean.getSupport_num();
            int zanCount = Integer.parseInt(support_num);
            if (size < 5) {
                holderView.home_all_shen.setVisibility(View.INVISIBLE);
                holderView.home_all_playtour.setVisibility(View.GONE);
            } else {
                holderView.home_all_shen.setVisibility(View.VISIBLE);
                holderView.home_all_playtour.setVisibility(View.GONE);
            }
            /*内容的图片*/
            if (infoBean.getAll_image() != null) {
                images = infoBean.getAll_image();
                holderView.my_circle_gv.setAdapter(new MyCircleDetailGridView(context, images, position));
                holderView.my_circle_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        MyCircleDetailGridView adapter = (MyCircleDetailGridView) adapterView.getAdapter();
                        int position1 = adapter.position;
                        List<String> all_image = mDataList.get(position1).getAll_image();
                        intent.setClass(context, InvitationDeatil_HeadViewGridView_Viewpager_Activity.class);
                        intent.putStringArrayListExtra("imagesViewpager", (ArrayList<String>) all_image);
                        AbSharedUtil.putInt(context, SharedConstants.IMAGE_INDEX, i);
                        context.startActivity(intent);
                    }
                });
            } else {
                holderView.my_circle_gv.setVisibility(View.GONE);
            }
            /*评论的条目内容*/
            List<CircleDetailBean.InfoBeanX.InfoBean.PCommentBean> pComment = infoBean.getPComment();
            if (pComment.size() > 5) {
                holderView.tv_examie_comment.setVisibility(View.VISIBLE);
            } else {
                holderView.tv_examie_comment.setVisibility(View.GONE);
            }
            CircleDetailListOneAdapter adapter = new CircleDetailListOneAdapter(context, position, mDataList);
            holderView.list_one.setAdapter(adapter);
            adapter.addAll(pComment);
              /*判断是不是群主*/
            String uid = infoBean.getUid();
            if (circle_uid.equals(uid)) {
                holderView.hoeme_all_nickname.setText(infoBean.getMember().getNickname());
                holderView.hoeme_all_nickname.setTextColor(context.getResources().getColor(R.color.circle_name));
            } else {
                holderView.hoeme_all_nickname.setText(infoBean.getMember().getNickname());
                holderView.hoeme_all_nickname.setTextColor(context.getResources().getColor(R.color.circle_names));
            }
        }
        /*下拉箭头*/
        holderView.home_all_classify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyToast.showToast(context, "请加入书圈后操作", Toast.LENGTH_SHORT);
            }
        });
        /*点赞*/
        holderView.home_all_zan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyToast.showToast(context, "请加入书圈后操作", Toast.LENGTH_SHORT);
            }
        });
        /*打赏*/
        holderView.home_all_playtour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyToast.showToast(context, "请加入书圈后操作", Toast.LENGTH_SHORT);
            }
        });
        /*查看评论*/
        holderView.tv_examie_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyToast.showToast(context, "请加入书圈后操作", Toast.LENGTH_SHORT);
            }
        });
        holderView.home_all_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyToast.showToast(context, "请加入书圈后操作", Toast.LENGTH_SHORT);
            }
        });

    }

    /*获取用户的userid*/
    public void getUserUid(String circle_uid) {
        this.circle_uid = circle_uid;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        /*头像*/
        private CircleImageView circleImageView;
        /*神贴*/
        private ImageView home_all_shen;
        /*昵称*/
        private TextView hoeme_all_nickname;
        /*发布时间*/
        private TextView home_all_releasetime;
        /*向下的箭头*/
        private ImageView home_all_classify;
        /*内容的文字*/
        private ExpandableTextView tv;
        /*内容的图片*/
        private NoScrollGridView my_circle_gv;
        /*打赏的图片*/
        private ImageView home_all_playtour;
        /*点赞的按钮*/
        private ImageView home_all_zan;
        /*赞的图片显示或隐藏*/
        private ImageView invitation_show_zan;
        /*显示点赞的人名*/
        private TextView ll_myclicle_detail_contentzan;
        /*显示评论的内容*/
        private ListViewAndListView list_one;
        /*点击查看更多*/
        private TextView tv_examie_comment;
        /*消息的图片*/
        private ImageView home_all_message;

        public MyViewHolder(View itemView) {
            super(itemView);
            circleImageView = (CircleImageView) itemView.findViewById(R.id.home_all_head);
            home_all_shen = (ImageView) itemView.findViewById(R.id.home_all_shen);
            hoeme_all_nickname = (TextView) itemView.findViewById(R.id.hoeme_all_nickname);
            home_all_releasetime = (TextView) itemView.findViewById(R.id.home_all_releasetime);
            home_all_classify = (ImageView) itemView.findViewById(R.id.home_all_classify);
            home_all_message = (ImageView) itemView.findViewById(R.id.home_all_message);
            tv = (ExpandableTextView) itemView.findViewById(R.id.tv);
            my_circle_gv = (NoScrollGridView) itemView.findViewById(R.id.my_circle_gv);
            home_all_playtour = (ImageView) itemView.findViewById(R.id.home_all_playtour);
            home_all_zan = (ImageView) itemView.findViewById(R.id.home_all_zan);
            invitation_show_zan = (ImageView) itemView.findViewById(R.id.invitation_show_zan);
            ll_myclicle_detail_contentzan = (TextView) itemView.findViewById(R.id.ll_myclicle_detail_contentzan);
            tv_examie_comment = (TextView) itemView.findViewById(R.id.tv_examie_comment);
            list_one = (ListViewAndListView) itemView.findViewById(R.id.list_one);
        }
    }

    List<String> zanNickName = new ArrayList<>();
    private int sizes;

    /*修改点赞的人数*/
    private void AlertLikeCount() {
        if (forumBookmark != null) {
            for (int zan = 0; zan < forumBookmark.size(); zan++) {
                String nickname = forumBookmark.get(zan).getNickname();
                zanNickName.add(nickname);
                sizes = zanNickName.size();
            }
            if (forumBookmark != null) {
                int size = zanNickName.size() > 12 ? 12 : sizes;
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < size; j++) {
                    if (j != size - 1) {
                        sb.append(zanNickName.get(j) + ",");
                    } else {
                        sb.append(zanNickName.get(j));
                    }
                    if (zanNickName.size() > 12) {
                        sb.append("等" + zanNickName.size() + "人已赞");
                    }
                    //holderView.ll_myclicle_detail_contentzan.setText(sb.toString());
                }
            }
        }
    }

    /*修改点赞的人数*/
    private String AlertLikeCount2(List<CircleDetailBean.InfoBeanX.InfoBean.ForumBookmarkBean> forumBookmark) {
        ArrayList<String> strings = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        if (forumBookmark != null) {
            for (int zan = 0; zan < forumBookmark.size(); zan++) {
                String nickname = forumBookmark.get(zan).getNickname();
                strings.add(nickname);
            }
            if (forumBookmark != null) {
                int size = strings.size() > 12 ? 12 : strings.size();
                for (int j = 0; j < size; j++) {
                    if (j != size - 1) {
                        sb.append(strings.get(j) + ", ");
                    } else {
                        sb.append(strings.get(j));
                    }
                    if (strings.size() > 12) {
                        sb.append("等" + strings.size() + "人已赞");
                    }
                }
            }
        }
        return sb.toString();
    }
}
